﻿using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Dapper.Model;
using Dapper.Test;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        /// <summary>
        /// 测试查询
        /// </summary>
        [TestMethod]
        public void TestQuery()
        {
            DbContext context = DbContext.Instance;
            string q1 = "SELECT * FROM PATIENT";
            IEnumerable<Patient> patients = context.conn.Query<Patient>(q1);
            Assert.AreEqual(patients.ToList().Count, 12);
        }

        /// <summary>
        /// 测试更新
        /// </summary>
        [TestMethod]
        public void TestUpdate()
        {
            DbContext context = DbContext.Instance;
            string q2 = "Update Patient Set Tel = '110'";
            var count = context.conn.Execute(q2);
            Assert.AreEqual(count, 12);
        }

        /// <summary>
        /// 测试新增
        /// </summary>
        [TestMethod]
        public void TestAdd()
        {
            DbContext context = DbContext.Instance;
            string q3 = "Insert Into Patient(Name,Sex,Status) Values(@Name,@Sex,@Status)";
            int count = context.conn.Execute(q3,new Patient{ Name = "dock",Sex=1,Status=1 });
            Assert.AreEqual(count, 1);
        }

        /// <summary>
        /// 测试删除
        /// </summary>
        [TestMethod]
        public void TestDelete()
        {
            DbContext context = DbContext.Instance;
            string q4 = "Delete from Patient Where ID = @ID";
            int count = context.conn.Execute(q4, new { ID = 14 });
            Assert.AreEqual(count, 1);
        }

        /// <summary>
        /// 测试多结果查询
        /// </summary>
        [TestMethod]
        public void TestNultiQuery()
        {
            DbContext context = DbContext.Instance;
            string q5 = "SELECT * FROM PATIENT;SELECT * FROM REPORT;";
            using (var multiQuery = context.conn.QueryMultiple(q5))
            {
                var patients = multiQuery.Read<Patient>().ToList();
                var reports = multiQuery.Read<Report>().ToList();

                Assert.AreEqual(patients.Where(i => i.ID == 3).FirstOrDefault().Name, "TEST");
                Assert.AreEqual(reports.Where(i => i.ID == 2).FirstOrDefault().Status,3);
            }
        }
    }
}
